<?php

declare(strict_types=1);

namespace Dreamcat\ApiDocTest\TestCases;

use Dreamcat\ApiDoc\ApiDefineGeneratorInterface;
use Dreamcat\ApiDoc\ApiDoc;
use Dreamcat\ApiDoc\DocSaverInterface;
use Dreamcat\ApiDoc\Popo\ApiDefinition;
use Dreamcat\ApiDoc\Popo\RouterEntranceDefinition;
use Dreamcat\ApiDoc\RouteDefineScanInterface;
use PHPUnit\Framework\TestCase;
use ReflectionException;
use ReflectionMethod;

/**
 * 集成测试
 * @author vijay
 */
class IntegrationTest extends TestCase
{
    /**
     * @throws ReflectionException
     */
    public function testIntegration()
    {
        $path = uniqid("project-path");
        $routerDefinition = new RouterEntranceDefinition();
        $routerDefinition->setUri(uniqid("/api/"))
            ->setMethod("POST")
            ->setAction(new ReflectionMethod($this, "testIntegration"));
        $apiDef = new ApiDefinition();
        $apiDef->setMethod($routerDefinition->getMethod())
            ->setUri($routerDefinition->getUri())
            ->setApiTag("test");

        $generator = $this->getMockForAbstractClass(ApiDefineGeneratorInterface::class);
        $scan = $this->getMockForAbstractClass(RouteDefineScanInterface::class);
        $saver = $this->getMockForAbstractClass(DocSaverInterface::class);

        $scan->expects(self::once())
            ->method("scanRouteList")
            ->with($path)
            ->willReturn([$routerDefinition]);
        $saver->expects(self::once())
            ->method("start")
            ->with();
        $generator->expects(self::once())
            ->method("parseApi")
            ->with($routerDefinition)
            ->willReturn($apiDef);
        $saver->expects(self::once())
            ->method("saveApiDefinition")
            ->with($apiDef);
        $saver->expects(self::once())
            ->method("end")
            ->with();

        $apiDoc = new ApiDoc($generator, $scan, $saver);
        $apiDoc->save($path);
    }
}

# end of file
